package com.groupThree.wx.controller;

import com.groupThree.bean.BootStrapTableExpress;
import com.groupThree.bean.Express;
import com.groupThree.bean.Message;
import com.groupThree.bean.User;
import com.groupThree.mvc.ResponseBody;
import com.groupThree.service.CourierService;
import com.groupThree.service.ExpressService;
import com.groupThree.util.DateFormatUtil;
import com.groupThree.util.JSONUtil;
import com.groupThree.util.UserUtil;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Stream;

public class ExpressController {
    @ResponseBody("/wx/fingExpressByUserPhone.do")
    public String fingExpressByUserPhone(HttpServletRequest request, HttpServletResponse response){
        User wxUser = UserUtil.getWxUser(request.getSession());
        String userPhone = wxUser.getU_phone();

        List<Express> list = ExpressService.findByUserPhone(userPhone);
        List<BootStrapTableExpress> list2 = new ArrayList<>();
        for(Express e:list){
            String inTime = DateFormatUtil.format(e.getInTime());
            String outTime = e.getOutTime()==null?"未出库":DateFormatUtil.format(e.getOutTime());
            String status = e.getStatus()==0?"待取件":"已取件";
            String code = e.getCode()==null?"已取件":e.getCode();
            BootStrapTableExpress e2 = new BootStrapTableExpress(e.getId(),e.getNumber(),e.getUsername(),e.getUserPhone(),e.getCompany(),code,inTime,outTime,status,e.getSysPhone());
            list2.add(e2);
        }
        Message msg = new Message();
        if(list.size()==0){
            msg.setStatus(-1);
            String json = JSONUtil.toJSON(msg);
            return json;
        }else{
            msg.setStatus(0);
            Stream<BootStrapTableExpress> status0Express = list2.stream().filter(express -> {
                if (express.getStatus().equals("待取件")) {
                    return true;
                } else {
                    return false;
                }
            }).sorted((o1,o2) -> {
                long o1Time = DateFormatUtil.toTime(o1.getInTime());
                long o2Time = DateFormatUtil.toTime(o2.getInTime());
                return (int)(o1Time-o2Time);
            });
            Stream<BootStrapTableExpress> status1Express = list2.stream().filter(express -> {
                if (express.getStatus().equals("已取件")) {
                    return true;
                } else {
                    return false;
                }
            }).sorted((o1,o2) -> {
                long o1Time = DateFormatUtil.toTime(o1.getInTime());
                long o2Time = DateFormatUtil.toTime(o2.getInTime());
                return (int)(o1Time-o2Time);
            });
            Object[] s0 = status0Express.toArray();
            Object[] s1 = status1Express.toArray();
            Map data = new HashMap<>();
            data.put("status0",s0);
            data.put("status1",s1);
            msg.setData(data);
            String json = JSONUtil.toJSON(msg.getData());
            return json;
        }



    }
    @ResponseBody("/wx/userExpressList.do")
    public String expressList(HttpServletRequest request,HttpServletResponse response){
        String userPhone = request.getParameter("userPhone");
        List<Express> list = ExpressService.findByUserPhoneAndStatus(userPhone, 0);
        List<BootStrapTableExpress> list2 = new ArrayList<>();
        for(Express e:list){
            String inTime = DateFormatUtil.format(e.getInTime());
            String outTime = e.getOutTime()==null?"未出库":DateFormatUtil.format(e.getOutTime());
            String status = e.getStatus()==0?"待取件":"已取件";
            String code = e.getCode()==null?"已取件":e.getCode();
            BootStrapTableExpress e2 = new BootStrapTableExpress(e.getId(),e.getNumber(),e.getUsername(),e.getUserPhone(),e.getCompany(),code,inTime,outTime,status,e.getSysPhone());
            list2.add(e2);
        }
        Message msg = new Message();
        if(list.size() == 0){
            msg.setStatus(-1);
            msg.setResult("未查询到快递");
        }else{
            msg.setStatus(0);
            msg.setResult("查询成功");
            msg.setData(list2);
        }
        return JSONUtil.toJSON(msg);
    }

    @ResponseBody("/wx/fingExpressByExpressNum.do")
    public String findExpress(HttpServletRequest request,HttpServletResponse response){
        String expressNum = request.getParameter("expressNum");
        System.out.println(expressNum);
        Express e = ExpressService.fidByNumber(expressNum);
        Message msg = new Message();
        if (e !=null){
            String inTime = DateFormatUtil.format(e.getInTime());
            String outTime = e.getOutTime()==null?"未出库":DateFormatUtil.format(e.getOutTime());
            String status = e.getStatus()==0?"待取件":"已取件";
            String code = e.getCode()==null?"已取件":e.getCode();
            BootStrapTableExpress e2 = new BootStrapTableExpress(e.getId(),e.getNumber(),e.getUsername(),e.getUserPhone(),e.getCompany(),code,inTime,outTime,status,e.getSysPhone());

            msg.setStatus(0);

            msg.setData(e2);
            msg.setResult("查询成功");
        }else {
            msg.setStatus(-1);
            msg.setResult("你输入的单号不存在，请检查单号输入是否正确！");
        }
        String json = JSONUtil.toJSON(msg);
        System.out.println(json);
        return json;


    }

    @ResponseBody("/wx/expressInsert.do")
    public String insert(HttpServletRequest request, HttpServletResponse response) {
        String number = request.getParameter("number");
        String company = request.getParameter("company");
        String username = request.getParameter("username");
        String userPhone = request.getParameter("userPhone");
        String sysPhone = UserUtil.getWxUser(request.getSession()).getU_phone();
        // TODO: 还没写柜子端，不存在录入人信息
        Express e = new Express(number, username, userPhone, company,sysPhone);
        System.out.println(e);
        Message msg = new Message();
        CourierService.updateDeliveries(sysPhone);
        boolean flag = ExpressService.insert(e);
        if (flag) {
            //成功
            msg.setStatus(0);
            msg.setResult("快递录入成功！");
        } else {
            msg.setStatus(-1);
            msg.setResult("快递录入失败！");
        }
        String json = JSONUtil.toJSON(msg);
        return json;

    }
}
